<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">+新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号" type="index">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name"></el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="setPower(row)"
                    >分配权限</el-button
                  >
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              @size-change="sizeChange"
              @current-change="currentChange"
              :current-page="page.page"
              :page-sizes="[1, 10, 20, 100]"
              :page-size="page.pagesize"
              layout="total, sizes,prev,pager,next,jumper"
              :total="page.total"
            >
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <component :is="'Info'"></component>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <DialogItem
      ref="dialogItem"
      :isShow.sync="show"
      :list="list"
      @currChange="currentChange"
      @getData="getData"
    ></DialogItem>
    <DialogPower :isShow.sync="showPower" ref="dialogPower"></DialogPower>
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from "@/api/setting.js";
import DialogItem from "./components/dialogItem.vue";
import DialogPower from "./components/dialogPower.vue";
export default {
  components: {
    DialogItem,
    DialogPower,
    Info: () => import("./components/info.vue"), //异步组件
  },
  data() {
    return {
      show: false,
      showPower: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 100,
      },
      list: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await sysRole(this.page);
      this.page.total = res.data.total;
      this.list = res.data.rows;
    },
    // 页码改变
    currentChange(page) {
      this.page.page = page;
      this.getData();
    },
    // 页容量改变
    sizeChange(size) {
      this.page.pagesize = size;
      this.page.page = 1;
      this.getData();
    },
    // 新增
    add() {
      this.show = true;
      // this.$refs.dialogItem.mode = 'add'
    },
    // 删除
    del(row) {
      this.$confirm("请确定删除该角色吗？", "提示")
        .then(async () => {
          await sysRoleDel(row.id);
          this.$message.success("删除成功");
          this.currentChange(1);
        })
        .catch(() => {});
    },
    // 编辑
    edit(row) {
      this.show = true;
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row));
      this.$refs.dialogItem.mode = "edit";
    },
    setPower(row) {
      this.showPower = true;
      this.$refs.dialogPower.getRoleInfo(row.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
  .pagination {
    text-align: right;
    padding-top: 15px;
  }
}
</style>
